package com.iotechn.unimall.admin.api.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.UserLevelDO;
import com.iotechn.unimall.data.mapper.UserLevelMapper;
import com.iotechn.unimall.data.mapper.UserTypeMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class AdminUserLevelServiceImpl implements AdminUserLevelService {

    @Autowired
    private UserLevelMapper userLevelMapper;
    @Override
    public Page<UserLevelDO> list(Integer page, Integer limit, String name, Long adminId) throws ServiceException {
        Wrapper<UserLevelDO> wrapper = new EntityWrapper<UserLevelDO>();
        wrapper.setSqlSelect("id", "`name`");
        if (!StringUtils.isEmpty(name))
            wrapper.like("name", name);
        wrapper.orderBy("id", false);
        Integer count = userLevelMapper.selectCount(wrapper);
        List<UserLevelDO> UserLevelDOS = userLevelMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(UserLevelDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserLevelDO create(UserLevelDO leaseCategoryDO, Long adminId) throws ServiceException {
        Wrapper<UserLevelDO> wrapper = new EntityWrapper<UserLevelDO>()
                .eq("name", leaseCategoryDO.getName());
        if (userLevelMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_PARTITION_CANNOT_REPEATED);
        if (userLevelMapper.insert(leaseCategoryDO) > 0) {
            leaseCategoryDO.setId(leaseCategoryDO.getId());
            return leaseCategoryDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(UserLevelDO leaseCategoryDO, Long adminId) throws ServiceException {
        Wrapper<UserLevelDO> wrapper = new EntityWrapper<UserLevelDO>()
                .eq("name", leaseCategoryDO.getName())
                .ne("id", leaseCategoryDO.getId());
        if (userLevelMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_PARTITION_CANNOT_REPEATED);
        Long id = leaseCategoryDO.getId();
        if (id == null) return "fail";
        if (userLevelMapper.updateById(leaseCategoryDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (userLevelMapper.deleteById(id) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public UserLevelDO detail(Long id, Long adminId) throws ServiceException {
        return userLevelMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
       if (userLevelMapper.delete(new EntityWrapper<UserLevelDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        return "ok";
    }

    @Override
    public List<UserLevelDO> select(Long adminId) {
        Wrapper<UserLevelDO> wrapper = new EntityWrapper<UserLevelDO>().setSqlSelect("id", "name").orderBy("id", false);
        return userLevelMapper.selectList(wrapper);
    }
}
